#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include "satellite.h"

static volatile bool running = true;

void signal_handler(int signum) {
    (void)signum;
    running = false;
}

void print_help() {
    printf("卫星姿态控制系统\n");
    printf("命令:\n");
    printf("  1 - 指向太阳\n");
    printf("  2 - 指向地球\n");
    printf("  3 - 指向天顶\n");
    printf("  4 - 稳定模式\n");
    printf("  5 - 姿态机动演示\n");
    printf("  s - 显示状态\n");
    printf("  q - 退出\n");
}

int main() {
    Satellite sat;
    ErrorCode result;
    
    // 设置信号处理
    signal(SIGINT, signal_handler);
    
    printf("=== 卫星姿态控制系统启动 ===\n");
    
    // 初始化卫星
    result = satellite_init(&sat, "LEO-SAT-001");
    if (result != SUCCESS) {
        printf("卫星初始化失败: %d\n", result);
        return 1;
    }
    
    // 初始状态
    satellite_print_status(&sat);
    
    print_help();
    
    char command[32];
    double simulation_time = 0.0;
    const double dt = 0.1;  // 100ms步长
    
    while (running) {
        printf("\n> ");
        fflush(stdout);
        
        if (fgets(command, sizeof(command), stdin) == NULL) {
            break;
        }
        
        // 移除换行符
        command[strcspn(command, "\n")] = 0;
        
        if (strlen(command) == 0) {
            // 空命令，继续模拟
            satellite_update(&sat, dt);
            simulation_time += dt;
            continue;
        }
        
        switch (command[0]) {
            case '1':
                // 指向太阳
                printf("执行太阳指向...\n");
                satellite_point_to_direction(&sat, vector3d_create(1.0, 0.0, 0.0));
                break;
                
            case '2':
                // 指向地球
                printf("执行地球指向...\n");
                satellite_point_to_direction(&sat, vector3d_negate(sat.position));
                break;
                
            case '3':
                // 指向天顶
                printf("执行天顶指向...\n");
                satellite_point_to_direction(&sat, vector3d_create(0.0, 0.0, 1.0));
                break;
                
            case '4':
                // 稳定模式
                printf("执行稳定模式...\n");
                satellite_stabilize(&sat);
                break;
                
            case '5':
                // 姿态机动演示
                printf("执行姿态机动演示...\n");
                Quaternion target = quaternion_from_euler(30.0 * DEG_TO_RAD, 
                                                          45.0 * DEG_TO_RAD, 
                                                          60.0 * DEG_TO_RAD);
                satellite_set_target_attitude(&sat, target);
                break;
                
            case 's':
                // 显示状态
                satellite_print_status(&sat);
                break;
                
            case 'q':
                running = false;
                break;
                
            case 'h':
                print_help();
                break;
                
            default:
                printf("未知命令: %s\n", command);
                print_help();
                break;
        }
        
        // 运行模拟
        if (command[0] != 'q') {
            printf("运行模拟...\n");
            for (int i = 0; i < 50 && running; i++) {
                satellite_update(&sat, dt);
                simulation_time += dt;
                
                // 每5秒打印一次状态
                if (i % 50 == 0) {
                    char state_str[256];
                    satellite_get_state_string(&sat, state_str, sizeof(state_str));
                    printf("\r%s", state_str);
                    fflush(stdout);
                }
            }
            printf("\n");
        }
    }
    
    printf("\n=== 模拟结束 ===\n");
    satellite_print_status(&sat);
    
    return 0;
}
